﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Phoenix.Xna.Framework.Components.Compositors
{
    public class ImageCompositor : Compositor
    {
        private string _asset;

        public string ImageAsset
        {
            get { return _asset; }
            set
            {
                if (!string.IsNullOrEmpty(_asset))
                {
                    Engine.Instance.Content.Release(_asset);
                }

                _asset = value;
                Image = Engine.Instance.Content.Load<Texture2D>(_asset);
                IsContentLoaded = true;
            }
        }
        protected Texture2D Image { get; set; }
        public Rectangle? SourceRectangle { get; set; }

        public ImageCompositor(string image)
            : base()
        {
            _asset = image;
        }

        public ImageCompositor(string id, string image)
            : base(id)
        {
            _asset = image;
        }

        #region Compositor Members

        public override void LoadContent()
        {
            if (!IsContentLoaded)
            {
                Image = Engine.Instance.Content.Load<Texture2D>(_asset);
                base.LoadContent();
            }
        }

        public override void UnloadContent()
        {
            if (IsContentLoaded)
            {
                Engine.Instance.Content.Release(_asset);
                base.UnloadContent();
            }
        }

        public override void Draw(GameTime gameTime)
        {
            if (Visible)
            {
                if (Image != null)
                {
                    SpriteBatch.Draw(Image, Position, SourceRectangle, Color, Rotation, Origin, Scale, Effect, LayerDepth);
                }
            }
        }

        #endregion
    }
}
